﻿using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Edu.Http.Extensions;
using Edu.Http.Proxy;
using Infrastructure.Config.Core.External;
using Infrastructure.ExternalService.Core.Product.Request;
using Infrastructure.ExternalService.Core.Product.Response;
using Infrastructure.ExternalService.Core.Responses;
using Infrastructure.ExternalService.Core.StudentService;
using Infrastructure.ExternalService.Core.StudentService.Request;
using Infrastructure.ExternalService.Core.StudentService.Response;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.Application.Dtos;

namespace Infrastructure.ExternalService.Core.Imp.StudentService
{
    public class StudentServiceClient : BaseHttpClient, IStudentServiceClient
    {
        private readonly OrderCenterApiOptions _orderingOptions;
        private readonly string formUrlencoded = "application/x-www-form-urlencoded";

        public StudentServiceClient(IOptions<OrderCenterApiOptions> options,
            HttpClient client, ILoggerFactory loggerFactory)
            : base(client, loggerFactory, options.Value.StudentServiceOptions.EndPoint)
        {
            _orderingOptions = options.Value ?? throw new HttpProxyException($"{nameof(StudentServiceClient)}配置获取失败");
            loggerFactory.CreateLogger<StudentServiceClient>();
        }

        /// <summary>
        /// 设置BaseHttpClient的BaseAddress：该值默认是从构造函数传入的，但重写该方法可以改变该值
        /// </summary>
        /// <returns></returns>
        protected override string SetHttpClientBaseAddress()
        {
            return null;
        }


        public async Task<List<OpenClassResponse>> OpenClose(OpenClassRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<WrapResponse<List<OpenClassResponse>>>(url: _orderingOptions.StudentServiceOptions.OpenClose, jsonStr: formStr, formUrlencoded, headers: null);

            if (response != null && response.Success && response.Data != null)
            {
                return response.Data;
            }

            throw new HttpProxyException($"{response.Msg}");
        }

    }
}
